//
//  LCHomePingLunTableViewController.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/11.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCHomePingLunTableViewController.h"

@interface LCHomePingLunTableViewController ()

@end

@implementation LCHomePingLunTableViewController


- (void)dealloc {
    NSLog(@"LCHomePingLunTableViewController dealloc");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"评论列表";
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    //2、去掉整个tableView的所有cell分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[LSZAppraiseTableViewCell class] forCellReuseIdentifier:@"LSZAppraiseTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
    // iOS7.0的特性 : 当滚动视图及其子类(UIScrollView/UIWebView/UITablewView/UICollectionView/UITexeView...)在自动布局时,遇到导航,里面的内容会自动的向下偏移64像素.
    if(@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.mutArrayComments = [NSMutableArray array];self.page = 1;
    [self getVideoCommentsList:self.page];
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreComments];
    }];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
    
    [self setupTableViewLayout];
}
- (void)setupTableViewLayout
{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-0);
        }
    }];
}

-(void)loadMoreComments{
    self.page++;
    [self getVideoCommentsList:self.page];//加载更多数据,翻页, 刷新UI
}
#pragma mark - 获取用户对资源的评论
-(void)getVideoCommentsList :(NSInteger)page {
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic GetTheResourceEvaluationList:self.id_id type:self.type page:[NSString stringWithFormat:@"%ld",(long)(long)page] :^(BOOL isOK, NSString *str, LSZResourceEvaluationListManager * manager) {
        if (isOK == YES) {
            if(page == 1){
                [weakSelf.mutArrayComments removeAllObjects];
                [weakSelf.mutArrayComments addObjectsFromArray:manager.mutMgArray];
            }else{
                [weakSelf.mutArrayComments addObjectsFromArray:manager.mutMgArray];
                if (manager.mutMgArray.count == 0) {
                    weakSelf.page = weakSelf.page-1;
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.51 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                    });
                }
            }
            if(page == 1){//刷新
                NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
                [weakSelf.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
            }else {
                [weakSelf.tableView reloadData];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.41 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.tableView.mj_footer endRefreshing];
            });
        }else{
        }
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mutArrayComments.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSZResourceEvaluationListMg * manager = nil;
    manager = self.mutArrayComments[indexPath.row];
    CGFloat intFont;
    if (ScreenWidth == 320) {
        //5
        intFont = 13.5;
    }else if(ScreenWidth == 375){
        // 6 7
        intFont = 14;
    }else {
        //plus
        intFont = 14.5;
    }
    CGFloat height = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth-30 height:10000 font:intFont :manager.content];
    int count;
    if([NSStr(manager.commentNum) isEqualToString:@"0"]||NSStr(manager.commentNum).length == 0||[NSStr(manager.commentNum) isEqualToString:@""]){
        count  = 0;
    }else{
        count = 1;
    }
    if (ScreenWidth == 320) {
        //5
        return height+56+count*29;
    }else if(ScreenWidth == 375){
        // 6 7
        return height+60+count*32.5;
    }else {
        //plus
        return height+64+count*34;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     LSZAppraiseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZAppraiseTableViewCell" forIndexPath:indexPath];
     [self contentWithPingJia:indexPath :cell];
     return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = [UIColor whiteColor];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return TAB_BAR_HEIGHT-49;
}
#pragma mark - __________________________评论__________________________________
#pragma mark - 评价的cellForRow
-(void)contentWithPingJia :(NSIndexPath *)indexPath :(LSZAppraiseTableViewCell *)cell
{
    LSZResourceEvaluationListMg * manager = self.mutArrayComments[indexPath.row];
    cell.ResourceEvaluationListMg = manager;
    cell.labelContent.userInteractionEnabled = YES;
    cell.butZan.tag = indexPath.row +1;
    [cell.butZan addTarget:self action:@selector(clickCommentsZan:) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *tapNumber = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCommentsNumber:)];
    [cell.labelZanNumber addGestureRecognizer:tapNumber];
    UIView *singleTapView = [tapNumber view];
    singleTapView.tag = indexPath.row+1;
    
    UITapGestureRecognizer *tapShou = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCommentsShou:)];
    [cell.imageViewShou addGestureRecognizer:tapShou];
    UIView *tapShouTapView = [tapShou view];
    tapShouTapView.tag = indexPath.row+1;
    
    UITapGestureRecognizer *tapNextPage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapNextPageComments:)];
    [cell.labelContent addGestureRecognizer:tapNextPage];
    UIView *tapNextPageView = [tapNextPage view];
    tapNextPageView.tag = indexPath.row+1;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    __weak typeof(self) weakSelf = self;
    [cell.labelNumberEvaluation tapGesture:^(UIGestureRecognizer *ges) {
      [weakSelf gotoNextPageComments:indexPath.row];
    }];
}
#pragma mark - 对某条评论点赞
-(void)clickCommentsZan:(UIButton *)but{
    [self initComments:but.tag];
}
-(void)tapCommentsNumber:(UITapGestureRecognizer *)tap{
    UITapGestureRecognizer *singleTap = (UITapGestureRecognizer *)tap;
    [self initComments:[singleTap view].tag];
}
-(void)tapCommentsShou:(UITapGestureRecognizer *)tap{
    UITapGestureRecognizer *singleTap = (UITapGestureRecognizer *)tap;
    [self initComments:[singleTap view].tag];
}
#pragma mark - 用户对评论点赞
-(void)initComments :(NSInteger)row{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    LSZResourceEvaluationListMg * manager = nil;
    manager = self.mutArrayComments[row-1];
    [LSZRequestData UsersCommentOnThumbUp:self.id_id comment_id:manager.id_id :^(BOOL isOK, NSString *str, NSDictionary * dic) {
        if (isOK == YES) {
            LSZResourceEvaluationListMg * manager = nil;
            manager = self.mutArrayComments[row-1];
            NSString * praiseNum = manager.praiseNum;
            NSInteger pnumber = [praiseNum integerValue]+1;
            manager.praiseNum = [NSString stringWithFormat:@"%ld",(long)pnumber];
            NSIndexPath *indexPath=[NSIndexPath indexPathForRow:row-1 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            [self setStringWarning:@"点赞成功!"];
        }else{
            [self setStringWarning:str];
        }
    }];
}
#pragma mark - hub的弹窗
-(void)setStringWarning:(NSString *)str {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;//枚举类型不同的效果
    hud.label.text= [NSString stringWithFormat:@"%@",str];
    [self performSelector:@selector(hiddenMBAction) withObject:nil afterDelay:1.0];
}
-(void)hiddenMBAction {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}


-(void)tapNextPageComments :(UITapGestureRecognizer *)tap
{
    UITapGestureRecognizer *singleTap = (UITapGestureRecognizer *)tap;
    [self gotoNextPageComments:[singleTap view].tag-1];
}
//-(void)tapNumberEvaluation:(UITapGestureRecognizer *)tap
//{
//    UITapGestureRecognizer *singleTap = (UITapGestureRecognizer *)tap;
//    [self gotoNextPageComments:[singleTap view].tag-1];
//}

#pragma mark - 评论的跳转时间
-(void)gotoNextPageComments :(NSInteger)row {
    LSZCommentDetailsVC * vc = [[LSZCommentDetailsVC alloc]init];
    LSZResourceEvaluationListMg * manager = nil;
    manager = self.mutArrayComments[row];
    vc.strHeaderImageView = manager.publisherHead;
    vc.strName = manager.publisher;
    vc.strZan = manager.praiseNum;
    vc.strTitle = manager.content;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //vc.strTitle = 
    });
    vc.strTime = manager.publishTime;
    vc.comment_id = manager.id_id;
    vc.id_id = self.id_id;
    vc.publisherId = manager.publisherId;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
